package com.spark.lola.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.feign.service.PortalServiceOpenApi;
import com.spark.lola.logistics.entity.CarInfo;
import com.spark.lola.logistics.entity.DriverInfo;
import com.spark.lola.logistics.mapper.DriverInfoMapper;
import com.spark.lola.logistics.service.DriverInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.mybatis.service.impl.BaseServiceImpl;
import com.spark.lola.support.wms.cache.CollectionCache;
import com.spark.lola.support.wms.configuration.WmsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/04/24 13:05
 * 驾驶员信息业务实现
 **/
@Service
public class DriverInfoServiceImpl extends BaseServiceImpl<DriverInfoMapper, DriverInfo> implements DriverInfoService {

    @Autowired
    private DriverInfoMapper driverInfoMapper;

    @Autowired
    private WmsProperties wmsProperties;

    @Override
    public IPage<DriverInfo> findPage(QueryParam<DriverInfo> queryParam) {
        PageParam<DriverInfo> pageParam = queryParam.toPageParam();
        return driverInfoMapper.findPage(pageParam.getPage(), pageParam.getEntityWrapper());
    }

    @Override
    public DriverInfo findInfo(long driverId) {
        return driverInfoMapper.findInfo(driverId);
    }

    @Override
    public boolean carIdExists(long carId) {
        return driverInfoMapper.countByCarId(carId) != 0;
    }

    @Override
    public boolean empIdExists(long empId) {
        return driverInfoMapper.countByEmpId(empId) != 0;
    }

    @Override
    public int bindCar(Long carId, int isBind, long driverId) {
        return driverInfoMapper.updateCarIdById(carId, isBind, driverId);
    }

    @Override
    public Long findCarId(long id) {
        return driverInfoMapper.findCarIdById(id);
    }

    @Override
    public List<SelectMap> selectMap(String key, int isBind) {
        QueryWrapper<DriverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("emp_id,real_name,car_id");
        queryWrapper.eq("used", CommonConstant.Y);
        queryWrapper.eq("is_bind", isBind);
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(o -> o.like("real_name", key));
        }
        List<DriverInfo> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps = new ArrayList<>();
        if (!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getEmpId().toString(), item.getRealName(), item.getCarId().toString()));
            });
        }
        return selectMaps;
    }

    @Override
    public List<SelectMap> findMap() {
        List<ShiroUser> shiroUsers = CollectionCache.dutyEmployees.get(wmsProperties.getLogistics().getDutyId().get(1));
        List<Long> empIdList = driverInfoMapper.findEmpId();
        List<SelectMap> selectMaps = new ArrayList<>();
        shiroUsers.forEach(shiroUser -> {
            if (!empIdList.contains(shiroUser.getId())) {
                selectMaps.add(new SelectMap(shiroUser.getId().toString(), shiroUser.getLoginNo(), shiroUser.getRealName() + "," + shiroUser.getPhone()));
            }
        });
        return selectMaps;
    }

}
